class Admin::PropertyPatternsController < Admin::AdminController
  before_filter :load_property_pattern
  #filter_resource_access

  add_crumb("Správa šablon vlastností", :only => :index) {}
  add_crumb("Správa šablon vlastností", :admin_property_patterns_path, :except => :index)
  add_crumb("Editace šablony", :only => [:edit, :update]) {}
  add_crumb("Nová šablona", :only => [:new, :create]) {}

  def index
    @search = PropertyPattern.search(params[:search])
    @property_patterns = @search.page(params[:page])
  end

  def new

  end

  def new_property
    @property = PropertyPatternProperty.new({
      :property_pattern_id => @property_pattern.id
    })

    @property.save!(:validate => false)

    @property_attribute = PropertyPatternPropertyAttribute.new({
      :property_pattern_property_id => @property.id
    })

    @property_attribute.save!(:validate => false)

    respond_to do |format|
      format.js {render :action => "new_property", :layout => false}
    end
  end

  def new_property_attribute
    @property = PropertyPatternProperty.find(params[:property_id])

    @property_attribute = PropertyPatternPropertyAttribute.new({
      :property_pattern_property_id => params[:property_id]
    })

    @property_attribute.save!(:validate => false)

    respond_to do |format|
      format.js {render :action => "new_property_attribute", :layout => false}
    end
  end

  def create
    if @property_pattern.save
      redirect_to edit_admin_property_pattern_path(@property_pattern), :notice => "Šablona vlastností byla úspěšně uložena"
    else
      render :action => :new
    end
  end

  def edit

  end

  def update
    if @property_pattern.update_attributes(params[:property_pattern])
      redirect_to edit_admin_property_pattern_path(@property_pattern), :notice => "Šablona vlastností byla úspěšně uložena"
    else
      render :action => :edit
    end
  end

  def restructure
    prev_id   = params[:prev_id].to_i unless params[:prev_id].blank?
    next_id   = params[:next_id].to_i unless params[:next_id].blank?
    node_id   = params[:node_id].to_i unless params[:node_id].blank?
    parent_id = params[:parent_id].to_i unless params[:parent_id].blank?

    prev_id ||= 0
    next_id ||= 0
    node_id ||= 0
    parent_id ||= 0

    property_attribute = PropertyPatternPropertyAttribute.find(node_id)

    if prev_id.zero? && next_id.zero?
      property_attribute.move_to_child_of parent_id
    elsif !prev_id.zero?
      property_attribute.move_to_right_of prev_id
    elsif !next_id.zero?
      property_attribute.move_to_left_of next_id
    end

    render :nothing => true
  end

  def destroy
    if @property_pattern.destroy
      redirect_to :admin_property_patterns_path, :notice => "Šablonu vlastností se podařilo úspěšně smazat."
    else
      flash[:error] = "Šablonu vlastností se nepodařilo smazat."
    end
  end

  private
  def load_property_pattern
    @property_pattern = PropertyPattern.find(params[:id]) unless params[:id].blank?
    @property_pattern ||= PropertyPattern.new(params[:property_pattern])
  end
end
